import { getBreadCrumbList, getHomeRoute, routeHasExist, setTagNavListInLocalstorage, getTagNavListFromLocalstorage } from '@/libs/router_utils.js'
import routers from '@/router/routers'

export default {
    state: {
        sidebar: false,
        tagNavList: [],
        breadCrumbList: [],
        homeRoute: getHomeRoute(routers)
    },

    getters: {
        sidebar: state => state.sidebar,
        menus: (state, getters) => routers
    },

    mutations: {
        setToggleSidebar: (state) => {
            state.sidebar = !state.sidebar
        },
        setTagNavList(state, list) {
            if (list) {
                state.tagNavList = [...list]
                setTagNavListInLocalstorage([...list])
            } else state.tagNavList = getTagNavListFromLocalstorage()
        },
        addTag(state, { route, type = 'unshift' }) {
            if (!routeHasExist(state.tagNavList, route)) {
                if (type === 'push') state.tagNavList.push(route)
                else {
                    if (route.name === 'home') state.tagNavList.unshift(route)
                    else state.tagNavList.splice(1, 0, route)
                }
                setTagNavListInLocalstorage([...state.tagNavList])
            }
        },
        setBreadCrumb(state, routeMetched) {
            state.breadCrumbList = getBreadCrumbList(routeMetched, state.homeRoute)
        },

    },

    actions: {
        handlerToggleSideBar({ commit, state }) {
            commit('setToggleSidebar')
        }
    }
}